﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Gof_Observer
{
    public class WeatherDataSubject : Subject
    {
        //记录观察者
        private IList<Observer> observers;
        private float temperature;  //温度
        private float humidity;// 湿度
        private float pressure;//压强

        public WeatherDataSubject() {
            observers = new List<Observer>();
        }

        #region Subject 成员

        public void registerObserver(Observer o)
        {
            observers.Add(o);
        }

        public void removeObserver(Observer o)
        {
            observers.Remove(o);
        }

        public void notifyObserver()
        {
            foreach (Observer item in observers)
            {
                item.update(temperature, humidity, pressure);
            }
        }
        #endregion
        /// <summary>
        /// measurements 译:测量值
        /// 当气象站得到更新观测值时,我们通知观察者
        /// </summary>
        public void measurementsChanged() {
            notifyObserver();
        }
        /// <summary>
        /// 设置最新的气象数据
        /// </summary>
        /// <param name="temperature"></param>
        /// <param name="humidity"></param>
        /// <param name="pressure"></param>
        public void setMeasurements(float temperature ,float  humidity ,float  pressure) {
            this.temperature = temperature;
            this.humidity = humidity;
            this.pressure = pressure;
            measurementsChanged();
        }
    }
}
